package com.qiye.livevideo.utils;


import com.qiye.livevideo.common.RetConstant;
import com.qiye.livevideo.model.AliCloudLiveInfo;
import com.qiye.livevideo.service.AliCloudLiveInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Created on 2019/4/29.
 *推流地址demo
 * @author meishijun
 */
@Component
public class AliLivePushFlowUtils {

    @Autowired
    private AliCloudLiveInfoService aliCloudLiveInfoService;
    private static AliLivePushFlowUtils aliLivePushFlowUtils;

    @PostConstruct
    public void init() {
        aliLivePushFlowUtils = this;
        aliLivePushFlowUtils.aliCloudLiveInfoService = this.aliCloudLiveInfoService;
    }

    public static AliCloudLiveInfo getLiveInfoByAccessKeyId(String accessKeyId){
        if(accessKeyId==null || accessKeyId.isEmpty()){
            accessKeyId = RetConstant.DEFAULT_ACCESS_KEY_ID;
        }
        AliCloudLiveInfo aliCloudLiveInfo = aliLivePushFlowUtils.aliCloudLiveInfoService.selectByaccessKeyId(accessKeyId);
        if(aliCloudLiveInfo!=null){
            return  aliCloudLiveInfo;
        }
        return new AliCloudLiveInfo();
    }

    /**
     * 获取推流地址
     * @param appName
     * @param streamName
     * @param time
     * @return
     */
    public static String getPushUrl(String appName,String streamName, Long time,String accessKeyId) {
        AliCloudLiveInfo aliCloudLiveInfo = getLiveInfoByAccessKeyId(accessKeyId);
        return "rtmp://"+ aliCloudLiveInfo.getPushDomainName() +getRoomName(appName,streamName) + generateAuthKey(appName,streamName,time,aliCloudLiveInfo.getPushDomainAuthKey());
    }

    /**
     * 应用名称和流名称
     * @param appName
     * @param streamName
     * @return
     */
    private static String getRoomName(String appName,String streamName) {
        return "/"+appName + "/"+streamName+"?";
    }

    /**
     * 完整验签串
     * @param appName
     * @param streamName
     * @param endTime
     * @return
     */
    private static String generateAuthKey(String appName,String streamName,Long endTime,String pushDomainAuthKey) {
        return "auth_key=" + endTime + generateUuid() + generateEncryptStr(appName,streamName,endTime,pushDomainAuthKey);
    }

    /**
     * 唯一标识
     * @return
     */
    private static String generateUuid() {
        String uuid = "0";
        String uid = "0";
        return "-" + uuid + "-" + uid + "-";
    }

    /**
     * 验签密钥
     * @param appName
     * @param streamName
     * @param endTime
     * @return
     */
    private static String generateEncryptStr(String appName,String streamName,Long endTime,String pushDomainAuthKey) {
        String md5push = "/"+appName+"/"+streamName+"-"+endTime+"-0-0-"+pushDomainAuthKey;
        return Md5Utils.getMd5Code(md5push);
    }

    /**
     * 将有效时间时间转换成时间戳
     * @param minute
     * @return
     */
    public  static Long dataToStamp(int minute){
        Long time = System.currentTimeMillis();
        //在当前系统的基础上增加分钟数。即
        time+= minute*1000*60;
        return time/1000;
    }

}
